package com.shelpe.services.algorithm.entity.mongo;

import java.util.Date;

import org.apache.commons.lang3.time.DateFormatUtils;
import org.springframework.data.annotation.Id;
import org.springframework.data.mongodb.core.index.Indexed;
import org.springframework.data.mongodb.core.mapping.Document;

import com.shelpe.services.algorithm.client.RankClient;
import com.shelpe.services.algorithm.entity.batis.Keyword;
import com.shelpe.services.algorithm.entity.jpa.DevelopmentKeyword;

import lombok.Data;

@Data
@Document(collection="keyword_development_log")
public class KeywordDevelopmentLog {
	@Id
	private String id;
	
	private DevelopmentKeyword target;
	private RankClient.KeywordRealtimeRankItem currentRank;
	private String action;
	private KeywordSnapshot snapshot;
	
	@Indexed
	private long keywordID;
	@Indexed
	private long userID;
	@Indexed
	private long containerID;
	@Indexed
	private long planID;
	@Indexed
	private Date logTime;
	
	private boolean result;
	
	public void setupID(long keywordID, Date time){
		this.id = keywordID + "-" +DateFormatUtils.format(time, "yyyyMMddHHmm");
	}
	
	public void setupOriginalKeyword(Keyword keyword){
		if(this.snapshot == null){
			this.snapshot = new KeywordSnapshot();
			this.snapshot.setKeywordID(keyword.getKeywordId());
			this.snapshot.setWord(keyword.getWord());
		}
		this.snapshot.setMatchScope(keyword.getMatchscope());
		this.snapshot.setMaxPrice(keyword.getMaxPrice());
		this.snapshot.setMaxMobileMaxPrice(keyword.getMaxMobilePrice());
	}
	public void setupNewKeyword(Keyword keyword){
		if(this.snapshot == null){
			this.snapshot = new KeywordSnapshot();
			this.snapshot.setKeywordID(keyword.getKeywordId());
			this.snapshot.setWord(keyword.getWord());
		}
		this.snapshot.setNewMatchScope(keyword.getMatchscope());
		this.snapshot.setNewMaxPrice(keyword.getMaxPrice());
		this.snapshot.setNewMobileMaxPrice(keyword.getMaxMobilePrice());
	}
	
	@Data
	public static class KeywordSnapshot{
		private long keywordID;
		private String word;
		private int maxPrice;
		private int maxMobileMaxPrice;
		private int matchScope;
		private int newMaxPrice;
		private int newMobileMaxPrice;
		private int newMatchScope;
	}
}
